﻿Imports System.IO
Imports System.IO.TextWriter
Imports System.Data.SqlServerCe



Public Class LevelDatabase
    Private mDbName As String = "LevelDatabase.sdf"
    Private localDbName As String = System.IO.Path.GetDirectoryName(Reflection.Assembly. _
        GetExecutingAssembly.GetName.CodeBase.ToString) & "\" & mDbName
    Private connStr As String = "Data Source=" & localDbName

    Private Shared strGetTableNames As String = _
   "SELECT TABLE_NAME" & _
   "  FROM Information_Schema.TABLES" & _
   " WHERE TABLE_TYPE = 'TABLE'"

    Private connDb As SqlCeConnection
    Private cmndDB As SqlCeCommand


    Sub New()
        Try
            If Not File.Exists(localDbName) Then CreateDatabase()
        Catch ex As Exception
            MsgBox("创建数据库出错 ！")
        End Try

        Try
            connDb = New SqlCeConnection(connStr)
            connDb.Open()

            If Not ExistTable("Level_Project") Then ProjectCreateTable()
            If Not ExistTable("Level_Current") Then CurrentCreateTable()
            If Not ExistTable("Level_I_Exam") Then IExamCreateTable()
            If Not ExistTable("Level_Workers") Then WorkersCreateTable()
            If Not ExistTable("Level_Instrument") Then InstrCreateTable()
            If Not ExistTable("Level_XianCha") Then
                XianChaCreateTable()
                XianChaPreSet()
            End If

        Catch ex As Exception
            MsgBox("打开数据库连接出错！")
        End Try


    End Sub
    Private Sub CreateDatabase()
        Dim bNewCreate As Boolean = False
        Try
            'If Not File.Exists(localDbName) Then
            Dim eng As SqlCeEngine = New SqlCeEngine(connStr)
            eng.CreateDatabase()
            bNewCreate = True
            'End If
        Catch ex As Exception
            If File.Exists(localDbName) And bNewCreate Then File.Delete(localDbName)
            MsgBox("创建数据库出错！")
        End Try
    End Sub
    Sub CloseConn()
        connDb.Close()
    End Sub


#Region "PrivateSub"
    Private Function GetTableNames() As String
        Dim TableNames As String = String.Empty
        Try
            Dim cmndDB As New SqlCeCommand(strGetTableNames, connDb)
            Dim dbRead As SqlCeDataReader = cmndDB.ExecuteReader

            While dbRead.Read
                If TableNames = String.Empty Then
                    TableNames = dbRead.GetString(0)
                Else
                    TableNames &= "," & dbRead.GetString(0)
                End If
            End While
            dbRead.Close()

        Catch ex As Exception
            MsgBox("查找表名出错！")
        End Try
        Return TableNames
    End Function

    Private Function ExistTable(ByVal TableName As String) As Boolean
        Try
            Dim Names() As String = GetTableNames.Split(",")
            For Each sN As String In Names
                If sN = TableName Then
                    Return True
                End If
            Next
        Catch ex As Exception

        End Try
        Return False
    End Function

    Private Sub BaseCreateTable(ByVal TableName As String, ByVal CreateTableStr As String)
        'If Not File.Exists(localDbName) Then CreateDatabase()
        If ExistTable(TableName) Then Return
        Try
            'If File.Exists(localDbName) Then
            Dim cmdCreateTable As New Data.SqlServerCe.SqlCeCommand(CreateTableStr, connDb)
            cmdCreateTable.CommandType = Data.CommandType.Text
            cmdCreateTable.ExecuteNonQuery()
            'End If
        Catch ex As Exception
            MsgBox("创建表出错:" & TableName)
        End Try
    End Sub

    Private Function BaseRowCount(ByVal TableName As String) As Long
        Dim cnt As Integer = 0
        Try
            'If File.Exists(localDbName) Then

            Dim SelectStr As String = "Select Count(*) From  " & TableName
            Dim cmndDB As New SqlCeCommand(SelectStr, connDb)

            cnt = cmndDB.ExecuteScalar

            'End If
        Catch ex As Exception
            MsgBox("计算数据行数出错！")
        End Try
        Return cnt
    End Function

    Private Function SearchReader(ByVal TableName As String, _
                          ByVal ColumnName As String, _
                          ByVal MeetValue As String) As SqlCeDataReader
        Try
            'If File.Exists(localDbName) Then
            Dim SelectStr As String = "Select * From  " & TableName _
                & " WHERE " & ColumnName & " = '" & MeetValue & "'"
            Dim cmndDB As New SqlCeCommand(SelectStr, connDb)

            Dim dbRead As Data.SqlServerCe.SqlCeDataReader = cmndDB.ExecuteReader
            Return dbRead
            'End If
        Catch ex As Exception
            'MsgBox("查找数据行出错！")
        End Try
        Return Nothing

    End Function
    Private Function SearchReader(ByVal TableName As String, _
                          ByVal ColumnName1 As String, ByVal MeetValue1 As String, _
                          ByVal ColumnName2 As String, ByVal MeetValue2 As String) As SqlCeDataReader
        Try
            'If File.Exists(localDbName) Then
            Dim SelectStr As String = "Select * From  " & TableName _
                & " WHERE " & ColumnName1 & " = '" & MeetValue1 & _
                "' And " & ColumnName2 & " = '" & MeetValue2 & "'"
            Dim cmndDB As New SqlCeCommand(SelectStr, connDb)

            Dim dbRead As Data.SqlServerCe.SqlCeDataReader = cmndDB.ExecuteReader
            Return dbRead
            ' End If
        Catch ex As Exception
            MsgBox("查找2数据行出错！")
        End Try
        Return Nothing

    End Function

    Private Sub BaseDeleteRow(ByVal DelStr As String)
        Try
            'If File.Exists(localDbName) Then

            Dim cmndDB As New Data.SqlServerCe.SqlCeCommand(DelStr, connDb)
            cmndDB.ExecuteNonQuery()

            ' End If
        Catch ex As Exception
            MsgBox("删除数据行出错！")
        End Try
    End Sub

    Private Function BaseReadColumns(ByVal TableName As String, ByVal ColumnNames As String, _
                                            Optional ByVal WhereString As String = "") As String
        Dim sTmp As String = String.Empty
        Try
            'If File.Exists(localDbName) Then
            'If Not ExistTable(TableName) Then Return String.Empty

            Dim SelectStr As String = "Select * From " & TableName & WhereString
            Dim cmndDB As New Data.SqlServerCe.SqlCeCommand(SelectStr, connDb)

            Dim dbRead As Data.SqlServerCe.SqlCeDataReader
            dbRead = cmndDB.ExecuteReader
            Dim cols() As String = ColumnNames.Split(",")
            While dbRead.Read
                Dim sUnit As String = String.Empty
                For I As Integer = 0 To cols.Length - 1
                    If sUnit = String.Empty Then
                        sUnit = dbRead.Item(cols(I))
                    Else
                        sUnit &= "," & dbRead.Item(cols(I))
                    End If
                Next
                If sTmp = String.Empty Then
                    sTmp = sUnit
                Else
                    sTmp &= vbCrLf & sUnit
                End If
            End While

            dbRead.Close()

            'End If
        Catch ex As Exception
            ' MsgBox("查找数据列出错！")
        End Try
        Return sTmp
    End Function

#End Region

#Region "Db_CurrentOption"

    Private CurrentTableName As String = "Level_Current"

    Structure CurrentOption
        Dim CurrentProject As String
        Dim RecordFile As String
        Dim CompareFile As String

        Dim Surveyor As String
        Dim Recordor As String
        Dim Examor As String

        Dim Weather As String

        Dim InstrNo As String
        Dim Staff1 As String
        Dim Staff2 As String
    End Structure

    Sub CurrentCreateTable()
        Dim buildTables As New System.Text.StringBuilder
        buildTables.Append("CREATE TABLE " & CurrentTableName & "(")
        buildTables.Append("CurrentID int identity(1,1) Primary Key NOT NULL")
        buildTables.Append(",CurrentItem nvarchar(20) not null UNIQUE ")
        buildTables.Append(",CurrentValue nvarchar(20) not null ")
        buildTables.Append(")")

        BaseCreateTable(CurrentTableName, buildTables.ToString)

    End Sub

    Sub CurrentRecord(ByVal mCurrentOpt As CurrentOption)
        With mCurrentOpt
            If Not .CompareFile Is Nothing Then CurrentRecordRow("CompareFile", .CompareFile)
            If Not .Examor Is Nothing Then CurrentRecordRow("Examor", .Examor)
            If Not .InstrNo Is Nothing Then CurrentRecordRow("InstrNo", .InstrNo)
            If Not .CurrentProject Is Nothing Then CurrentRecordRow("CurrentProject", .CurrentProject)
            If Not .RecordFile Is Nothing Then CurrentRecordRow("RecordFile", .RecordFile)
            If Not .Recordor Is Nothing Then CurrentRecordRow("Recordor", .Recordor)
            If Not .Staff1 Is Nothing Then CurrentRecordRow("Staff1", .Staff1)
            If Not .Staff2 Is Nothing Then CurrentRecordRow("Staff2", .Staff2)
            If Not .Surveyor Is Nothing Then CurrentRecordRow("Surveyor", .Surveyor)
            If Not .Weather Is Nothing Then CurrentRecordRow("Weather", .Weather)
        End With
    End Sub

    Sub CurrentRecordRow(ByVal CurrentItem As String, ByVal CurrentItemValue As String)
        Try
            ' If File.Exists(localDbName) Then
            'CurrentCreateTable()


            Dim dbRead As SqlCeDataReader = SearchReader _
                (CurrentTableName, "CurrentItem", CurrentItem)
            Dim RowStr As New System.Text.StringBuilder

            If dbRead.Read Then
                dbRead.Close()
                RowStr.Append("")
                RowStr.Append("UPDATE " & CurrentTableName & " SET ")
                RowStr.Append("CurrentValue=" & "'" & CurrentItemValue & "'")
                RowStr.Append(" WHERE  CurrentItem = '" & CurrentItem & "'")

            Else
                '增加记录
                RowStr.Append("INSERT INTO " & CurrentTableName)
                RowStr.Append("(CurrentItem,CurrentValue) VALUES ")
                RowStr.Append("('" & CurrentItem & "','" & CurrentItemValue & "')")
            End If


            Dim cmdInsertRow As New Data.SqlServerCe.SqlCeCommand(RowStr.ToString, connDb)
            cmdInsertRow.CommandType = Data.CommandType.Text
            cmdInsertRow.ExecuteNonQuery()



            ' End If
        Catch ex As Exception

        End Try

    End Sub

    Function CurrentRead() As CurrentOption
        Dim mOpt As New CurrentOption
        With mOpt
            Dim sTmp As String
            sTmp = CurrentRead("CompareFile")
            If Not sTmp = String.Empty Then .CompareFile = sTmp

            sTmp = CurrentRead("Examor")
            If Not sTmp = String.Empty Then .Examor = sTmp

            sTmp = CurrentRead("InstrNo")
            If Not sTmp = String.Empty Then .InstrNo = sTmp

            sTmp = CurrentRead("CurrentProject")
            If Not sTmp = String.Empty Then .CurrentProject = sTmp

            sTmp = CurrentRead("RecordFile")
            If Not sTmp = String.Empty Then .RecordFile = sTmp

            sTmp = CurrentRead("Recordor")
            If Not sTmp = String.Empty Then .Recordor = sTmp

            sTmp = CurrentRead("Staff1")
            If Not sTmp = String.Empty Then .Staff1 = sTmp

            sTmp = CurrentRead("Staff2")
            If Not sTmp = String.Empty Then .Staff2 = sTmp

            sTmp = CurrentRead("Surveyor")
            If Not sTmp = String.Empty Then .Surveyor = sTmp

            sTmp = CurrentRead("Weather")
            If Not sTmp = String.Empty Then .Weather = sTmp
        End With
        Return mOpt
    End Function

    Function CurrentRead(ByVal CurrentItem As String) As String
        Dim WhereStr As String = " WHERE CurrentItem='" & CurrentItem & "'"
        Return BaseReadColumns(CurrentTableName, "CurrentValue", WhereStr)
    End Function

    Function CurrentRowCount()
        Return BaseRowCount(CurrentTableName)
    End Function

    Sub CurrentDeleteRow(ByVal CurrentProjectName As String)
        Dim DelStr As String = "DELETE FROM " & CurrentTableName & " WHERE CurrentProject='" & CurrentProjectName & "'"
        BaseDeleteRow(DelStr)
    End Sub

#End Region

#Region "Db_I_Exam"
    Public IExamTableName As String = "Level_I_Exam"

    Sub IExamCreateTable()
        Dim buildTables As New System.Text.StringBuilder
        buildTables.Append("CREATE TABLE " & IExamTableName & "(")
        buildTables.Append("ExamID int identity(1,1) Primary Key NOT NULL")
        buildTables.Append(",InstrNo nvarchar(10) not null")
        buildTables.Append(",IValue nvarchar(10) not null")
        buildTables.Append(",sDate nvarchar(10) not null")
        buildTables.Append(",Staff1 nvarchar(8) not null")
        buildTables.Append(",Staff2 nvarchar(8)")
        buildTables.Append(",Surveyor nvarchar(10) not null")
        buildTables.Append(",Recordor nvarchar(10) not null")
        buildTables.Append(",Examor nvarchar(10) not null")
        buildTables.Append(",ReadA1 nvarchar(100) not null")
        buildTables.Append(",ReadB1 nvarchar(100) not null")
        buildTables.Append(",ReadA2 nvarchar(100) not null")
        buildTables.Append(",ReadB2 nvarchar(100) not null")
        buildTables.Append(")")

        BaseCreateTable(IExamTableName, buildTables.ToString)
    End Sub

    Sub IExamRecordRow(ByRef mExam As clsExamI)
        Try
            'If File.Exists(localDbName) Then
            'IExamCreateTable()

            '当天是否有记录，有则改写，否则增加
            Dim dbRead As SqlCeDataReader = Me.SearchReader _
                (IExamTableName, "sDate", mExam.Opts.sDate)
            Dim RowStr As New System.Text.StringBuilder

            If dbRead.Read Then
                Dim examID As Integer = dbRead.Item("ExamID")
                dbRead.Close()
                '改写记录
                RowStr.Append("UPDATE " & IExamTableName & " SET ")
                With mExam.Opts
                    RowStr.Append("InstrNo=" & "'" & .InstrNo & "'")
                    RowStr.Append(", IValue=" & "'" & mExam.I_of_Level & "'")
                    RowStr.Append(", sDate=" & "'" & .sDate & "'")
                    RowStr.Append(", Staff1=" & "'" & .Staff1 & "'")
                    RowStr.Append(", Staff2=" & "'" & .Staff2 & "'")
                    RowStr.Append(", Surveyor=" & "'" & .Surveyor & "'")
                    RowStr.Append(", Recordor=" & "'" & .Recorder & "'")
                    RowStr.Append(", Examor=" & "'" & .Examor & "'")
                    RowStr.Append(", ReadA1=" & "'" & mExam.Stn1.ReadA.ToString & "'")
                    RowStr.Append(", ReadB1=" & "'" & mExam.Stn1.ReadB.ToString & "'")
                    RowStr.Append(", ReadA2=" & "'" & mExam.Stn2.ReadA.ToString & "'")
                    RowStr.Append(", ReadB2=" & "'" & mExam.Stn2.ReadB.ToString & "'")
                    RowStr.Append(" WHERE  ExamID=" & examID)
                End With
            Else
                '增加记录
                RowStr.Append("INSERT INTO " & IExamTableName)
                RowStr.Append(" (InstrNo,IValue,sDate,Staff1,Staff2,Surveyor,Recordor,Examor,ReadA1,ReadB1,ReadA2,ReadB2) VALUES ")
                RowStr.Append("(")
                With mExam.Opts
                    RowStr.Append("'" & .InstrNo & "'")
                    RowStr.Append(", '" & mExam.I_of_Level & "'")
                    RowStr.Append(", '" & .sDate & "'")
                    RowStr.Append(", '" & .Staff1 & "'")
                    RowStr.Append(", '" & .Staff2 & "'")
                    RowStr.Append(", '" & .Surveyor & "'")
                    RowStr.Append(", '" & .Recorder & "'")
                    RowStr.Append(", '" & .Examor & "'")
                    RowStr.Append(", '" & mExam.Stn1.ReadA.ToString & "'")
                    RowStr.Append(", '" & mExam.Stn1.ReadB.ToString & "'")
                    RowStr.Append(", '" & mExam.Stn2.ReadA.ToString & "'")
                    RowStr.Append(", '" & mExam.Stn2.ReadB.ToString & "'")
                End With
                RowStr.Append(")")
            End If

            Dim cmdInsertRow As New Data.SqlServerCe.SqlCeCommand(RowStr.ToString, connDb)
            Dim str As String = RowStr.ToString
            cmdInsertRow.CommandType = Data.CommandType.Text
            cmdInsertRow.ExecuteNonQuery()


            'End If
        Catch ex As Exception

        End Try

    End Sub

    Sub IExamReadLast(ByRef mExam As clsExamI)
        Try
            'If File.Exists(localDbName) Then
            'If Not ExistTable(IExamTableName) Then Return



            Dim SelectStr As String = "Select * From  " & IExamTableName
            Dim cmndDB As New Data.SqlServerCe.SqlCeCommand(SelectStr, connDb)

            Dim dbRead As Data.SqlServerCe.SqlCeDataReader
            dbRead = cmndDB.ExecuteReader

            '跳过前面RowCount-2个
            For I As Integer = 0 To IExamRowCount() - 1 - 1
                dbRead.Read()
            Next
            '余下为最后一个
            If dbRead.Read Then SetExamIValue(dbRead, mExam)

            dbRead.Close()

            'End If
        Catch ex As Exception

        End Try

    End Sub

    Sub IExamRead(ByVal sTime As Integer, ByRef mExam As clsExamI)
        mExam = New clsExamI
        Try
            'If File.Exists(localDbName) Then
            'If Not ExistTable(IExamTableName) Then Return

            Dim SelectStr As String = "Select * From " & IExamTableName & " WHERE ExamID=" & sTime.ToString
            Dim cmndDB As New Data.SqlServerCe.SqlCeCommand(SelectStr, connDb)

            Dim dbRead As Data.SqlServerCe.SqlCeDataReader
            dbRead = cmndDB.ExecuteReader
            While dbRead.Read
                SetExamIValue(dbRead, mExam)
            End While

            dbRead.Close()

            'End If
        Catch ex As Exception

        End Try

    End Sub
    Private Sub SetExamIValue(ByRef dbRead As Data.SqlServerCe.SqlCeDataReader, ByRef IExam As clsExamI)

        With IExam.Opts
            .InstrNo = dbRead.Item("InstrNo")
            .sDate = dbRead.Item("sDate")
            .iNumber = dbRead.Item("ExamID")
            .Staff1 = dbRead.Item("Staff1")
            .Staff2 = dbRead.Item("Staff2")
            .Surveyor = dbRead.Item("Surveyor")
            .Recorder = dbRead.Item("Recordor")
            .Examor = dbRead.Item("Examor")
        End With
        With IExam
            SplitReads(.Stn1.ReadA, dbRead.Item("ReadA1"))
            SplitReads(.Stn1.ReadB, dbRead.Item("ReadB1"))
            SplitReads(.Stn2.ReadA, dbRead.Item("ReadA2"))
            SplitReads(.Stn2.ReadB, dbRead.Item("ReadB2"))
        End With

    End Sub
    Private Sub SplitReads(ByRef mRead As IExamRead, ByVal Reads As String)
        If Reads Is Nothing Then Return
        Dim tmp() As String = Reads.Split(vbCrLf)
        For Each sp As String In tmp
            Dim tt() As String = sp.Trim.Split(",")
            If tt.Length = 2 Then
                If IsNumeric(tt(1)) Then
                    mRead.AddRead(CLng(tt(1)), tt(0))
                End If
            End If
        Next
    End Sub

    Function IexamReadIvalues(Optional ByVal InstrNo As String = "") As String
        Dim Columns As String = "ExamID,InstrNo,sDate,IValue"
        Dim WhereString As String = IIf(InstrNo = "", "", " WHERE InstrNo='" & InstrNo & "'")
        Return Me.BaseReadColumns(IExamTableName, Columns, WhereString)
    End Function

    Sub IExamDeleteRow(ByVal ExamID As Integer)
        Dim DelStr As String = "DELETE FROM " & IExamTableName & " WHERE ExamID=" & ExamID
        Me.BaseDeleteRow(DelStr)
    End Sub

    Function IExamRowCount() As Long
        Return Me.BaseRowCount(IExamTableName)
    End Function

#End Region


#Region "Db_Workers"



    Private WorkwesTableName As String = "Level_Workers"

    Sub WorkersCreateTable()
        Dim buildTables As New System.Text.StringBuilder
        buildTables.Append("CREATE TABLE " & WorkwesTableName & "(")
        buildTables.Append("WorkerID int identity(1000,1) Primary Key NOT NULL")
        buildTables.Append(",WorkerName nvarchar(10) not null UNIQUE ")
        buildTables.Append(")")

        Me.BaseCreateTable(WorkwesTableName, buildTables.ToString)
    End Sub

    Sub WorkwersRecordRow(ByVal WorkerName As String)
        Try
            ' If File.Exists(localDbName) Then
            'WorkersCreateTable()


            '是否有此人，有则退出，否则增加
            Dim dbRead As SqlCeDataReader = Me.SearchReader _
                (WorkwesTableName, "WorkerName", WorkerName)
            Dim RowStr As New System.Text.StringBuilder

            If dbRead.Read Then
                dbRead.Close()
            Else
                '增加记录
                RowStr.Append("INSERT INTO " & WorkwesTableName)
                RowStr.Append("(WorkerName) VALUES ")
                RowStr.Append("('" & WorkerName & "')")

                Dim cmdInsertRow As New Data.SqlServerCe.SqlCeCommand(RowStr.ToString, connDb)
                cmdInsertRow.CommandType = Data.CommandType.Text
                cmdInsertRow.ExecuteNonQuery()
            End If

            ' End If
        Catch ex As Exception

        End Try

    End Sub

    Function WorkersRead() As String
        Return Me.BaseReadColumns(WorkwesTableName, "WorkerName")
    End Function

    Function WorkersRowCount()
        Return Me.BaseRowCount(WorkwesTableName)
    End Function

    Sub WorkersDeleteRow(ByVal WorkerName As String)
        Dim DelStr As String = "DELETE FROM " & WorkwesTableName & " WHERE WorkerName='" & WorkerName & "'"
        Me.BaseDeleteRow(DelStr)
    End Sub


#End Region

#Region "Db_Instrument"


    Private InstrTableName As String = "Level_Instrument"

    Sub InstrCreateTable()
        Dim buildTables As New System.Text.StringBuilder
        buildTables.Append("CREATE TABLE " & InstrTableName & "(")
        buildTables.Append("InstrmentID int identity(2000,1) Primary Key NOT NULL")
        buildTables.Append(",InstrType nvarchar(16) not null ")
        buildTables.Append(",InstrNumber nvarchar(16) not null UNIQUE ")
        buildTables.Append(")")

        Me.BaseCreateTable(InstrTableName, buildTables.ToString)
    End Sub

    Sub InstrRecordRow(ByVal InstrType As String, ByVal InstrName As String)
        Try
            'If File.Exists(localDbName) Then
            'InstrCreateTable()

            '是否有此仪器，有则退出，否则增加
            Dim dbRead As SqlCeDataReader = Me.SearchReader _
                (InstrTableName, "InstrType", InstrType, "InstrNumber", InstrName)
            Dim RowStr As New System.Text.StringBuilder

            If dbRead.Read Then
                dbRead.Close()
            Else
                '增加记录
                RowStr.Append("INSERT INTO " & InstrTableName)
                RowStr.Append(" (InstrType,InstrNumber) VALUES ")
                RowStr.Append(" ('" & InstrType & "'," & "'" & InstrName & "')")

                Dim cmdInsertRow As New Data.SqlServerCe.SqlCeCommand(RowStr.ToString, connDb)
                cmdInsertRow.CommandType = Data.CommandType.Text
                cmdInsertRow.ExecuteNonQuery()
            End If

            'End If
        Catch ex As Exception

        End Try

    End Sub


    Function InstrRead(Optional ByVal InstrType As String = "") As String
        Dim WhereStr As String = IIf(InstrType = "", "", " WHERE InstrType=" & "'" & InstrType & "'")
        Dim Columns As String = "InstrType,InstrNumber"
        Dim sTMp As String = Me.BaseReadColumns(InstrTableName, Columns, WhereStr)
        Return sTMp
    End Function

    Function InstrRowCount()
        Return Me.BaseRowCount(InstrTableName)
    End Function

    Sub InstrDeleteRow(ByVal InstrType As String, ByVal InstrName As String)
        Dim DelStr As String = "DELETE FROM " & InstrTableName & _
                              " WHERE InstrType='" & InstrType & "'" & _
                              " And InstrNumber='" & InstrName & "'"
        Me.BaseDeleteRow(DelStr)

    End Sub



#End Region

#Region "Db_XianCha"



    Private XianChaTableName As String = "Level_XianCha"

    Structure XianCha
        Dim Grade As String
        Dim MinRead As Single
        Dim TwoReadsErr As Single
        Dim HeightInTwoReadsErr As Single
        Dim Distance As Single
        Dim StnDistErr As Single
        Dim TotalDistErr As Single
    End Structure

    Private Sub XianChaPreSet()
        Dim mXC As New XianCha
        Dim sTmp As String = Read_XC_Grade()
        Dim I As Integer = InStr(sTmp, "二级")
        If InStr(sTmp, "二级") <= 0 Then
            '二等
            With mXC
                .Grade = "二级"
                .Distance = 50
                .MinRead = 0.3
                .HeightInTwoReadsErr = 0.0005
                .TwoReadsErr = 0.0007
                .StnDistErr = 2
                .TotalDistErr = 3
            End With
            XianChaRecordRow(mXC)
        End If

        If InStr(sTmp, "三级") <= 0 Then
            '三等
            With mXC
                .Grade = "三级"
                .Distance = 75
                .MinRead = 0.2
                .HeightInTwoReadsErr = 0.0007
                .TwoReadsErr = 0.001
                .StnDistErr = 3
                .TotalDistErr = 5
            End With
            XianChaRecordRow(mXC)
        End If

        If InStr(sTmp, "自定义") <= 0 Then
            '自定义
            With mXC
                .Grade = "自定义"
                .Distance = 100
                .MinRead = 0
                .HeightInTwoReadsErr = 0.001
                .TwoReadsErr = 0.0015
                .StnDistErr = 5
                .TotalDistErr = 10
            End With
            XianChaRecordRow(mXC)
        End If



    End Sub

    Sub XianChaCreateTable()
        Dim buildTables As New System.Text.StringBuilder
        buildTables.Append("CREATE TABLE " & XianChaTableName & "(")
        buildTables.Append("Grade nvarchar(6) Primary Key NOT NULL")
        buildTables.Append(",MinRead float not null ")
        buildTables.Append(",TwoReadsErr float not null ")
        buildTables.Append(",HeightInTwoReadsErr float not null ")
        buildTables.Append(",Distance float not null ")
        buildTables.Append(",StnDistErr float not null ")
        buildTables.Append(",TotalDistErr float not null ")
        buildTables.Append(")")

        Me.BaseCreateTable(XianChaTableName, buildTables.ToString)
    End Sub

    Sub XianChaRecordRow(ByVal XC As XianCha)
        Try
            'If File.Exists(localDbName) Then
            'XianChaCreateTable()



            '是否有此等级，有则改写，否则增加
            Dim dbRead As SqlCeDataReader = Me.SearchReader _
                (XianChaTableName, "Grade", XC.Grade)
            Dim RowStr As New System.Text.StringBuilder

            If dbRead.Read Then
                'Dim examID As Integer = dbRead.Item("ExamID")
                dbRead.Close()

                '改写记录
                RowStr.Append("UPDATE " & XianChaTableName & " SET ")
                With XC

                    RowStr.Append(" MinRead=" & "'" & .MinRead & "'")
                    RowStr.Append(", TwoReadsErr=" & "'" & .TwoReadsErr & "'")
                    RowStr.Append(", HeightInTwoReadsErr=" & "'" & .HeightInTwoReadsErr & "'")
                    RowStr.Append(", Distance=" & "'" & .Distance & "'")
                    RowStr.Append(", StnDistErr=" & "'" & .StnDistErr & "'")
                    RowStr.Append(", TotalDistErr=" & "'" & .TotalDistErr & "'")
                    RowStr.Append(" WHERE Grade=" & "'" & .Grade & "'")
                End With
            Else
                '增加记录
                RowStr.Append("INSERT INTO " & XianChaTableName)
                RowStr.Append(" (Grade,MinRead,TwoReadsErr,HeightInTwoReadsErr,Distance,StnDistErr,TotalDistErr) VALUES ")
                RowStr.Append("(")
                With XC
                    RowStr.Append("'" & .Grade & "'")
                    RowStr.Append(", '" & .MinRead & "'")
                    RowStr.Append(", '" & .TwoReadsErr & "'")
                    RowStr.Append(", '" & .HeightInTwoReadsErr & "'")
                    RowStr.Append(", '" & .Distance & "'")
                    RowStr.Append(", '" & .StnDistErr & "'")
                    RowStr.Append(", '" & .TotalDistErr & "'")
                End With
                RowStr.Append(")")
            End If

            Dim cmdInsertRow As New Data.SqlServerCe.SqlCeCommand(RowStr.ToString, connDb)
            Dim str As String = RowStr.ToString
            cmdInsertRow.CommandType = Data.CommandType.Text
            cmdInsertRow.ExecuteNonQuery()


            ' End If
        Catch ex As Exception

        End Try

    End Sub

    Function XianChaRead(ByVal Grade As String, ByRef XC As XianCha) As String
        Dim sTmp As String = String.Empty
        'If File.Exists(localDbName) Then
        'If Not ExistTable(XianChaTableName) Then Return String.Empty


        Dim SelectStr As String = "Select * From " & XianChaTableName & " WHERE Grade='" & Grade & "'"
        Dim cmndDB As New Data.SqlServerCe.SqlCeCommand(SelectStr, connDb)

        Dim dbRead As Data.SqlServerCe.SqlCeDataReader
        dbRead = cmndDB.ExecuteReader

        If dbRead.Read Then
            SetXianChaValue(dbRead, XC)
        End If
        dbRead.Close()

        'End If
        Return sTmp
    End Function


    Private Sub SetXianChaValue(ByRef dbRead As Data.SqlServerCe.SqlCeDataReader, ByRef XC As XianCha)
        XC = New XianCha
        With XC
            .Grade = dbRead.Item("Grade")
            .MinRead = dbRead.Item("MinRead")
            .TwoReadsErr = dbRead.Item("TwoReadsErr")
            .HeightInTwoReadsErr = dbRead.Item("HeightInTwoReadsErr")
            .Distance = dbRead.Item("Distance")
            .StnDistErr = dbRead.Item("StnDistErr")
            .TotalDistErr = dbRead.Item("TotalDistErr")
        End With
    End Sub

    Function Read_XC_Grade() As String
        Return Me.BaseReadColumns(XianChaTableName, "Grade")
    End Function

    Function XianChaRowCount()
        Return Me.BaseRowCount(XianChaTableName)
    End Function

    Sub XianChaDeleteRow(ByVal Grade As String)
        Dim DelStr As String = "DELETE FROM " & XianChaTableName & _
                              " WHERE Grade='" & Grade & "'"
        Me.BaseDeleteRow(DelStr)
    End Sub



#End Region

#Region "Project"



    Private ProjectTableName As String = "Level_Project"

    Structure ProjectData
        Dim ProjectID As Integer
        Dim ProjectName As String
        Dim BMs As List(Of LvlPoint)
        Dim MidReadUnit As Double
        Dim UpDownReadUnit As Double
        Dim DistReadMod As Integer
        Dim DistReadUnit As Double
        Dim JiFuCha As Double
        Dim DistMult As Double
        Dim XC_Grade As String
    End Structure

    Sub ProjectCreateTable()
        Dim buildTables As New System.Text.StringBuilder
        buildTables.Append("CREATE TABLE " & ProjectTableName & "(")
        buildTables.Append("ProjectID int identity(1,1) Primary Key NOT NULL")
        buildTables.Append(",ProjectName nvarchar(12)  not null UNIQUE")
        buildTables.Append(",BMs nvarchar(100) ")
        buildTables.Append(",MidReadUnit float ")
        buildTables.Append(",UpDownReadUnit float ")
        buildTables.Append(",DistReadMod integer ")
        buildTables.Append(",DistReadUnit float ")
        buildTables.Append(",JiFuCha float ")
        buildTables.Append(",DistMult float ")
        buildTables.Append(",XC_Grade nvarchar(6) ")
        buildTables.Append(")")

        BaseCreateTable(ProjectTableName, buildTables.ToString)
    End Sub

    Sub AddProjectRow(ByVal ProjectName As String)
        Try
            'If File.Exists(localDbName) Then
            'ProjectCreateTable()



            '当天是否有记录，有则改写，否则增加
            Dim dbRead As SqlCeDataReader = SearchReader _
                (ProjectTableName, "ProjectName", ProjectName)
            Dim RowStr As New System.Text.StringBuilder
            If dbRead.Read Then
                '已有该工程
                Dim examID As Integer = dbRead.Item("ExamID")
                dbRead.Close()

            Else
                '增加记录
                RowStr.Append("INSERT INTO " & ProjectTableName)
                RowStr.Append(" (ProjectName) VALUES ")
                RowStr.Append("('" & ProjectName & "')")

            End If

            Dim cmdInsertRow As New Data.SqlServerCe.SqlCeCommand(RowStr.ToString, connDb)
            Dim str As String = RowStr.ToString
            cmdInsertRow.CommandType = Data.CommandType.Text
            cmdInsertRow.ExecuteNonQuery()


            'End If
        Catch ex As Exception
            'MsgBox("eee")
        End Try

    End Sub

    Sub SaveProjectOpt(ByVal ProjectName As String, ByRef ProjectOpt As ProjectData)
        Try
            'If File.Exists(localDbName) Then
            'ProjectCreateTable()

            '是否有此等级，有则改写，否则增加
            Dim dbRead As SqlCeDataReader = SearchReader _
                (ProjectTableName, "ProjectName", ProjectName)
            If Not dbRead.Read Then
                AddProjectRow(ProjectName)
                dbRead.Close()
            End If
            '改写记录
            Dim RowStr As New System.Text.StringBuilder
            RowStr.Append("UPDATE " & ProjectTableName & " SET ")
            With ProjectOpt
                RowStr.Append(" BMs=" & "'" & BmsToString(.BMs) & "'")
                RowStr.Append(", MidReadUnit=" & "" & .MidReadUnit & "")
                RowStr.Append(", UpDownReadUnit=" & "" & .UpDownReadUnit & "")
                RowStr.Append(", DistReadMod=" & "" & .DistReadMod & "")
                RowStr.Append(", DistReadUnit=" & "" & .DistReadUnit & "")
                RowStr.Append(", JiFuCha=" & "" & .JiFuCha & "")
                RowStr.Append(", DistMult=" & "" & .DistMult & "")
                RowStr.Append(", XC_Grade=" & "'" & .XC_Grade & "'")
                RowStr.Append(" WHERE ProjectName=" & "'" & ProjectName & "'")
            End With


            Dim cmdInsertRow As New Data.SqlServerCe.SqlCeCommand(RowStr.ToString, connDb)
            Dim str As String = RowStr.ToString
            cmdInsertRow.CommandType = Data.CommandType.Text
            cmdInsertRow.ExecuteNonQuery()


            'End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub


    ''' <summary>
    ''' 读取一项工程
    ''' </summary>
    ''' <param name="ProjectName"></param>
    ''' <param name="mProject"></param>
    ''' <remarks></remarks>
    Sub ProjectRead(ByVal ProjectName As String, ByRef mProject As ProjectData)
        mProject = New ProjectData
        Try
            'If File.Exists(localDbName) Then
            'If Not ExistTable(ProjectTableName) Then Return

            Dim SelectStr As String = "Select * From " & ProjectTableName & " WHERE ProjectName='" & ProjectName & "'"
            Dim cmndDB As New Data.SqlServerCe.SqlCeCommand(SelectStr, connDb)

            Dim dbRead As Data.SqlServerCe.SqlCeDataReader
            dbRead = cmndDB.ExecuteReader
            While dbRead.Read
                SetProjectValue(dbRead, mProject)
            End While

            dbRead.Close()

            'End If
        Catch ex As Exception

        End Try

    End Sub
    Private Sub SetProjectValue(ByRef dbRead As Data.SqlServerCe.SqlCeDataReader, ByRef mProject As ProjectData)
        With mProject
            .ProjectName = dbRead.Item("ProjectName")
            .BMs = setBMsByString(dbRead.Item("BMs"))
            .MidReadUnit = dbRead.Item("MidReadUnit")
            .UpDownReadUnit = dbRead.Item("UpDownReadUnit")
            .DistReadMod = dbRead.Item("DistReadMod")
            .DistReadUnit = dbRead.Item("DistReadUnit")
            .JiFuCha = dbRead.Item("JiFuCha")
            .DistMult = dbRead.Item("DistMult")
            .XC_Grade = dbRead.Item("XC_Grade")
        End With
    End Sub

    Function setBMsByString(ByVal BMsStr As String) As List(Of LvlPoint)
        Dim Bms As New List(Of LvlPoint)
        If Not BMsStr = String.Empty Then
            Dim tmp() As String = BMsStr.Split(vbCrLf)
            For Each sp As String In tmp
                sp = sp.Replace(",", ":")
                Dim tt() As String = sp.Trim.Split(":")
                If tt.Length = 2 Then
                    If IsNumeric(tt(1)) Then
                        Dim vP As New LvlPoint(tt(0), tt(1))
                        Bms.Add(vP)
                    End If
                End If
            Next
        End If
        Return Bms
    End Function

    Function BmsToString(ByVal BMs As List(Of LvlPoint)) As String
        Dim sTmp As String = String.Empty
        For Each bm As LvlPoint In BMs
            If sTmp = String.Empty Then
                sTmp = bm.Name & "," & bm.Level.ToString
            Else
                sTmp &= vbCrLf & bm.Name & "," & bm.Level.ToString
            End If
        Next
        Return sTmp
    End Function

    ''' <summary>
    ''' 读取所有工程编号
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function ProjectRead() As String
        Dim Columns As String = "ProjectName"
        Return Me.BaseReadColumns(ProjectTableName, Columns)
    End Function

    ''' <summary>
    ''' 删除工程编号
    ''' </summary>
    ''' <param name="ProjcetName"></param>
    ''' <remarks></remarks>
    Sub ProjectDeleteRow(ByVal ProjcetName As String)
        Dim DelStr As String = "DELETE FROM " & ProjectTableName & " WHERE ProjectName='" & ProjcetName & "'"
        Me.BaseDeleteRow(DelStr)
    End Sub

    ''' <summary>
    ''' 工程项目数
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function ProjectRowCount() As Long
        Return Me.BaseRowCount(ProjectTableName)
    End Function

#End Region

End Class





#Region "I角"
''' <summary>
''' I角测读数据管理
''' </summary>
''' <remarks></remarks>
Public Class clsExamI
    Public Stn1 As New IExamStn
    Public Stn2 As New IExamStn
    Public Opts As New ExamIOptions

    Structure ExamIOptions
        Dim sDate As String
        Dim iNumber As Integer
        Dim Surveyor As String
        Dim Recorder As String
        Dim Examor As String
        Dim InstrNo As String
        Dim Staff1 As String
        Dim Staff2 As String

        Function Copy() As ExamIOptions
            Dim pNew As New ExamIOptions
            With pNew
                .sDate = Me.sDate
                .iNumber = Me.iNumber
                .Surveyor = Me.Surveyor
                .Recorder = Me.Recorder
                .Examor = Me.Examor
                .InstrNo = Me.InstrNo
                .Staff1 = Me.Staff1
                .Staff2 = Me.Staff2
            End With
            Return pNew
        End Function

    End Structure

    Sub New()
        Opts.sDate = ""
        Opts.Examor = ""
        Opts.Recorder = ""
        Opts.Staff1 = ""
        Opts.Staff2 = ""
        Opts.Surveyor = ""
        Opts.InstrNo = ""
        Opts.iNumber = 0

    End Sub

    ''' <summary>
    ''' 增加读数
    ''' </summary>
    ''' <param name="StnNo"></param>
    ''' <param name="Staff"></param>
    ''' <param name="Read"></param>
    ''' <remarks></remarks>
    Public Sub AddRead(ByVal StnNo As Integer, ByVal Staff As String, ByVal Read As Long)
        If StnNo = 1 Then
            Stn1.AddRead(Staff, Read)
        ElseIf StnNo = 2 Then
            Stn2.AddRead(Staff, Read)
        End If
    End Sub

    ''' <summary>
    ''' 删除读数
    ''' </summary>
    ''' <param name="StnNo"></param>
    ''' <param name="Staff"></param>
    ''' <param name="iPos"></param>
    ''' <remarks></remarks>
    Public Sub EraseRead(ByVal StnNo As Integer, ByVal Staff As String, ByVal iPos As Integer)
        If StnNo = 1 Then
            Stn1.EraseRead(Staff, iPos)
        ElseIf StnNo = 2 Then
            Stn2.EraseRead(Staff, iPos)
        End If
    End Sub

    ''' <summary>
    ''' 水准仪I角
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function I_of_Level() As String
        Dim h1 As Double = Stn1.Height
        Dim h2 As Double = Stn2.Height
        If h1 > -9000 And h2 > -9000 Then
            Return ((h2 - h1) * 5000).ToString("0.0")
        Else
            Return ""
        End If
    End Function

    ''' <summary>
    ''' 输出字串
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function ToString() As String
        Dim sTmp As String = String.Empty
        sTmp &= "            水准仪i角检测手簿" & vbCrLf & vbCrLf & vbCrLf

        With Opts
            sTmp &= "仪器:" & .InstrNo & "".PadLeft(11, " "c)
            sTmp &= "标尺:" & .Staff1 & "+" & .Staff2 & vbCrLf
            sTmp &= "日期:" & .sDate
            sTmp &= "".PadLeft(10, " "c) & "成像:清" & vbCrLf & vbCrLf

        End With


        sTmp &= "测站1" & vbCrLf
        sTmp &= Stn1.ToString & vbCrLf & vbCrLf

        sTmp &= "测站2" & vbCrLf
        sTmp &= Stn2.ToString & vbCrLf & vbCrLf

        sTmp &= "实测i角    "
        sTmp &= "i=(H2-H1)*5= " & I_of_Level() & vbCrLf & vbCrLf & vbCrLf

        With Opts
            sTmp &= "观测:" & .Surveyor.PadRight(10, " "c)
            sTmp &= "记录:" & .Recorder.PadRight(10, " "c)
            sTmp &= "检查:" & .Examor
        End With

        Return sTmp
    End Function

End Class

''' <summary>
''' I角检查的测站
''' </summary>
''' <remarks></remarks>
Public Class IExamStn

    Public ReadA As New IExamRead
    Public ReadB As New IExamRead

    ''' <summary>
    ''' 增加读数
    ''' </summary>
    ''' <param name="StaffNumber"></param>
    ''' <param name="Read"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function AddRead(ByVal StaffNumber As String, ByVal Read As Long) As Boolean
        If StaffNumber = "A" Then
            Return ReadA.AddRead(Read)
        ElseIf StaffNumber = "B" Then
            Return ReadB.AddRead(Read)
        End If
    End Function

    ''' <summary>
    ''' 删除读数
    ''' </summary>
    ''' <param name="StaffNumber"></param>
    ''' <param name="iPos"></param>
    ''' <remarks></remarks>
    Sub EraseRead(ByVal StaffNumber As String, ByVal iPos As Integer)
        If StaffNumber = "A" Then
            ReadA.EraseRead(iPos)
        ElseIf StaffNumber = "B" Then
            ReadB.EraseRead(iPos)
        End If
    End Sub

    ''' <summary>
    ''' 测站高差
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function Height() As Double
        Dim rA As Double = ReadA.AverageReadWithMeter
        Dim rB As Double = ReadB.AverageReadWithMeter
        If rA > 0 And rB > 0 Then
            Height = (rA - rB)
        Else
            Height = -9999
        End If

    End Function

    ''' <summary>
    ''' 字串输出
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function ToString() As String
        Dim sTmp As String = String.Empty
        sTmp &= "序号,  读数,    时间,  序号,  读数,    时间" & vbCrLf
        For I As Integer = 0 To Math.Max(ReadA.ReadCount, ReadB.ReadCount) - 1
            If I < ReadA.ReadCount Then
                With ReadA.ReadList(I)
                    sTmp &= " A_" & (I + 1).ToString & ","
                    sTmp &= .Read.ToString("000000") & ","
                    sTmp &= .Time & ","
                End With
            Else
                sTmp &= "                     "

            End If
            If I < ReadB.ReadCount Then
                With ReadB.ReadList(I)
                    sTmp &= "   B_" & (I + 1).ToString & ","
                    sTmp &= .Read.ToString("000000") & ","
                    sTmp &= .Time
                End With
            Else
                sTmp &= "                       "
            End If

            sTmp &= vbCrLf

        Next
        sTmp &= " A均:" & ReadA.AverageRead.ToString("000000") & "             B均:" & ReadB.AverageRead.ToString("000000") & vbCrLf
        sTmp &= "高差:" & (Height() * 1000).ToString("0.00mm")

        Return sTmp
    End Function


End Class

''' <summary>
''' 标尺读数  的类ISvRead
''' </summary>
''' <remarks></remarks>
Public Class IExamRead
    Structure ReadAndTime
        Dim Read As Long
        Dim Time As String

        Public Overrides Function ToString() As String
            Return Time & "," & Read.ToString
        End Function
    End Structure

    Public ReadList As New List(Of ReadAndTime)
    Const KtoMeter As Double = 0.00001

    ReadOnly Property ReadCount() As Integer
        Get
            Return ReadList.Count
        End Get
    End Property

    ReadOnly Property Reads() As List(Of Long)
        Get
            Dim mReads As New List(Of Long)
            For Each vP As ReadAndTime In ReadList
                mReads.Add(vP.Read)
            Next
            Return mReads
        End Get
    End Property


    ''' <summary>
    ''' 增加读数
    ''' </summary>
    ''' <param name="Read"></param>
    ''' <remarks></remarks>
    Function AddRead(ByVal Read As Long) As Boolean
        Dim rt As New ReadAndTime
        rt.Read = Read
        rt.Time = Now.ToString("hh:mm:ss")
        If ReadList.Count = 0 Then
            ReadList.Add(rt)
            Return True
        ElseIf ReadList.Count > 5 Then
            Return False
        ElseIf Math.Abs(Read - AverageRead()) < 0.0005 / KtoMeter Then
            ReadList.Add(rt)
            Return True
        End If
        Return False
    End Function

    ''' <summary>
    ''' 增加读数与时间
    ''' </summary>
    ''' <param name="Read"></param>
    ''' <param name="Time"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function AddRead(ByVal Read As Long, ByVal Time As String) As Boolean
        Dim rt As New ReadAndTime
        rt.Read = Read
        rt.Time = Time
        If ReadList.Count = 0 Then
            ReadList.Add(rt)
            Return True
        ElseIf Math.Abs(Read - AverageRead()) < 0.0005 / KtoMeter Then
            ReadList.Add(rt)
            Return True
        End If
        Return False

    End Function

    ''' <summary>
    ''' 删除读数
    ''' </summary>
    ''' <param name="iPos"></param>
    ''' <remarks></remarks>
    Sub EraseRead(ByVal iPos As Integer)
        If iPos > -1 And iPos < ReadList.Count Then
            ReadList.RemoveAt(iPos)
        End If
    End Sub

    ''' <summary>
    ''' 平均读数,正常为6位
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function AverageRead() As Long
        If ReadList.Count > 0 Then
            Dim Total As Double = 0
            For Each iRead As ReadAndTime In ReadList
                Total += iRead.Read
            Next
            Return Total / ReadList.Count
        Else
            Return -9999
        End If
    End Function

    ''' <summary>
    ''' 平均读数，以米为单位
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function AverageReadWithMeter() As Double
        Dim av As Long = AverageRead()
        Return IIf(av > 0, AverageRead() * KtoMeter, -9999)
    End Function

    Public Overrides Function ToString() As String
        Dim mStr As String = String.Empty
        Dim Cnt As Integer = 0
        For Each vR As ReadAndTime In ReadList
            If Cnt > 5 Then Exit For
            If mStr.Length = 0 Then
                mStr = vR.ToString
            Else
                mStr &= vbCrLf & vR.ToString
            End If
        Next
        Return mStr
    End Function




End Class

#End Region